package org.csu.mypetstoreplus.controller;

import org.csu.mypetstoreplus.common.BaseResponse;
import org.csu.mypetstoreplus.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
public class CartController {

    @Autowired
    CartService cartService;

    //获取当前用户的Cart
    @GetMapping("/carts")
    public BaseResponse<Object> getCart() {
        return BaseResponse.createForSuccess(cartService.getCart());
    }

    //清空当前用户的Cart
    @DeleteMapping("/carts")
    public BaseResponse<Object> deleteCart() {
        cartService.deleteCart();
        return BaseResponse.createForSuccess();
    }

    //添加Item到Cart
    @PostMapping("/cartItems")
    public BaseResponse<Object> addCartItem(@RequestParam String itemId, @RequestParam int quantity) {
        cartService.addCartItem(itemId, quantity);
        return BaseResponse.createForSuccess();
    }

    //从Cart中删除或减去一个Item
    @PutMapping("/cartItems")
    public BaseResponse<Object> updateCartItem(@RequestParam String itemId, @RequestParam int quantity) {
        cartService.updateCartItem(itemId, quantity);
        return BaseResponse.createForSuccess();
    }
}
